function MonkeyTalkWidget() {
  
  this.pingState = "none";
  this.sessionId = "<?php echo session_id() ?>";
  this.messages = [];
  this.state = "hidden";
  this.operatorStatus = "online";
  this.operatorName = "Ruslan";
  this.operatorAvatar = "";
  this.serverUrl = "http://lowcost/visitor/ajax/ping.php";
  this.interval = <?php echo CFG_PING_INTERVAL ?>;
  this.params = "";

  this.getElement = function(id) {
    var element = document.getElementById(id);
    
    if(!element) {
      alert("MonkeyTalkWidget.getElement: no element with id #" + id);
    }
    element.widget = this;
    return element;
  }  
    
  this.hideWidget = function() {
    try {
      this.setParams("windowState=hidden");
      this.widgetContainer.style.height = "70px";
    } catch(e) {
      alert("MonkeyTalkWidget.hideWidget: " + e);
    }
  }
  
  this.showWidget = function() {
    try {
      this.setParams("windowState=shown");
      this.widgetContainer.style.height = "440px";
    } catch (e) {
      alert("MonkeyTalkWidget.showWidget: " + e);
    }
  }

  this.addMessage = function(message) {
    this.messages[this.messages.length] = message;
    this.widgetMessages.innerHTML += "<div class=\"MonkeyTalkWidgetMessageItem\">" + decodeURI(message[0]) + ": " + decodeURI(message[1]) + "</div>";
  }
  
  this.onMessageReceived = function(message) {
    
  }  
  
  this.ping = function () {
    try {
      this.removePingScript();
      document.getElementsByTagName("head")[0].appendChild(this.createPingScript());
      if (this.pingState == "processing") {
        alert("Now answer from previous ping is received");
      } else {
        this.setPingState("processing");
        setTimeout("document.monkeyTalkWidget.ping()", this.interval * 1000);
      }
      this.params = "";
    } catch(e) {
      alert("MonkeyTalkWidget.ping: " + e);
    }    
  }

  /**
   * Removes ping script element from body
   */
  this.removePingScript = function() {
    var pingScript = document.getElementById("monkeyTalkPing");
    if (pingScript) {
      document.getElementsByTagName("head")[0].removeChild(pingScript);
    }    
  }
  
  /**
   * Creates and returns SCRIPT element for ping script
   */
  this.createPingScript = function() {
      var pingScript = document.createElement("script");
          pingScript.setAttribute("type", "text/javascript");
          pingScript.setAttribute("src", this.serverUrl + "?timestamp=" + new Date().getTime() + "&sessionId=" + this.sessionId + this.params);
          pingScript.setAttribute("id", "monkeyTalkPing");
          
     return pingScript;    
  }
  
  this.createWidgetElement = function() {

    var styleElement = document.createElement("style");
        /**
        styleElement.innerHTML = "#MonkeyTalkWidget{position:absolute;right:20px;bottom:0px;}body>div#MonkeyTalkWidget{position:fixed;}";
        */
        
    document.getElementsByTagName("body")[0].appendChild(styleElement);
    
    var widgetElement = document.createElement("div");
        widgetElement.setAttribute("id", "MonkeyTalkWidget");
        
        widgetElement.innerHTML = 
          "<div id=\"MonkeyTalkBadge\">"
        + "<span id=\"MonkeyTalkBadgeName\">Tanya Inozemtseva</span>"
        + "</div>"
        + "<div id=\"MonkeyTalkMessages\"></div>"
        + "<textarea id=\"MonkeyTalkMessage\"></textarea>"
        + "<a href=\"#\" id=\"MonkeyTalkWidgetHideButton\">hide</a>";
        
    document.getElementsByTagName("body")[0].appendChild(widgetElement);
  }
  
  /**
   * Sets state of currend ping iteration: default - none
   * When ping is in progress: processing
   * When ping is received: ok 
   */
  this.setPingState = function(state) {
    this.pingState = state;
  }
  
  /**
   * Sets additional parms for ping: format: a=b&c=d
   */
  this.setParams = function(params) {
    this.params = this.params + "&" + params;
  }
    
  this.sendMessage = function() {
    try {
      this.setParams("message=" + encodeURIComponent(this.widgetMessage.value));
      this.widgetMessage.value = "";
    } catch(e) {
      alert("MonkeyTalkWidget.sendMessage " + e);
    }        
  }
    
  this.onMessageReceived = function(message) {
    this.showWidget();  
  }
  
  /**
   * Widget Initialization Method 
   */
  this.init = function() {
    document.monkeyTalkWidget = this;
    this.createWidgetElement();
    
    this.widgetContainer = this.getElement("MonkeyTalkWidget");
    this.widgetMessages = this.getElement("MonkeyTalkMessages");
    this.widgetMessage = this.getElement("MonkeyTalkMessage");
    this.widgetBadge = this.getElement("MonkeyTalkBadge");  
    this.MonkeyTalkWidgetHideButton = this.getElement("MonkeyTalkWidgetHideButton");

    this.MonkeyTalkWidgetHideButton.onclick = function() {
      this.widget.hideWidget();
    }
    
    this.widgetMessage.onkeydown = function(e) {
      var keycode;
      if (window.event) {
        keycode = window.event.keyCode;
      } else {
        keycode = e.which;
      }
      if (keycode == 13) {
        this.widget.sendMessage();
        return false;      
      }
    }
    
    this.widgetBadge.onclick = function() {
      this.widget.showWidget();
    }    
    
    this.ping();
    <?php if ($visitor['windowState'] == 'hidden'): echo "this.hideWidget();"; endif ?>        
  }
  
  this.init();
}

var monkeyTalkWidget = new MonkeyTalkWidget();
